package initialize

import (
	"fmt"
	"gitee.com/DonHz/electronic-business-systems/mxshop_srvs/order_srv/global"
	"gitee.com/DonHz/electronic-business-systems/mxshop_srvs/order_srv/proto"
	_ "github.com/mbobakov/grpc-consul-resolver"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func InitSrvConn() {
	consulInfo := global.ServerConfig.ConsulInfo
	goodsConn, err := grpc.NewClient(fmt.Sprintf("consul://%s:%d/%s?wait=14s",
		consulInfo.Host, consulInfo.Port, global.ServerConfig.GoodsSrvInfo.Name),
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		zap.L().Error("【InitSrvConn】连接【商品服务】失败", zap.Error(err))
		panic(err)
	}
	goodsSrvClient := proto.NewGoodsClient(goodsConn)
	global.GoodsSrvClient = goodsSrvClient

	InvConn, err := grpc.NewClient(fmt.Sprintf("consul://%s:%d/%s?wait=14s",
		consulInfo.Host, consulInfo.Port, global.ServerConfig.InventorySrvInfo.Name),
		grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		zap.L().Error("【InitSrvConn】连接【库存服务】失败", zap.Error(err))
		panic(err)
	}
	InvSrvClient := proto.NewInventoryClient(InvConn)
	global.InventorySrvClient = InvSrvClient
}
